package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.MyException;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult list(SensitiveDto dto) {
        if (dto == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        dto.checkParam();
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        if (dto.getName() != null)
            wrapper.like(AdSensitive::getSensitives, dto.getName());
        IPage iPage = new Page(dto.getPage(), dto.getSize());
        IPage page = page(iPage, wrapper);
        PageResponseResult pageResponseResult = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page);
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive sensitive) {
        if (sensitive == null || sensitive.getSensitives() == null || "".equals(sensitive.getSensitives()))
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        if (getOne(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, sensitive.getSensitives())) != null)
            throw new MyException("该敏感词已存在不可以重复创建");
        if (sensitive.getCreatedTime() == null || "".equals(sensitive.getCreatedTime()))
            sensitive.setCreatedTime(new Date());
        save(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive sensitive) {
        if (sensitive == null || sensitive.getSensitives() == null || "".equals(sensitive.getSensitives()))
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        if (getOne(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getId, sensitive.getId())) == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        update(sensitive, Wrappers.<AdSensitive>lambdaUpdate().eq(AdSensitive::getId, sensitive.getId()));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        if (getById(id) == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
